<%
Class oauth
    Private OAUTH_CONSUMER_KEY
    Private OAUTH_CONSUMER_SECRET
	Private CALLBACK_URL
	Private SCOPE
    '新浪微博
	Private Sub wbInitialize      
        OAUTH_CONSUMER_KEY = "1206707256"'APP ID
        OAUTH_CONSUMER_SECRET = "4b151c8de5087a780da38e5dab1bacf3"'APP KEY
        CALLBACK_URL = "http://sorry.com/u.asp?c=wb"'REDIRECT_URI
		SCOPE =""
    End Sub
	'腾讯微博
	Private Sub qtInitialize      
        OAUTH_CONSUMER_KEY = "801130512"'APP ID
        OAUTH_CONSUMER_SECRET = "f38c4827e2b6e97627a40e77f6314a6a"'APP KEY
        CALLBACK_URL = "http://sorry.com/u.asp?c=qt"'REDIRECT_URI
		SCOPE ="all"
    End Sub
	'豆瓣
    Private Sub dbInitialize      
        OAUTH_CONSUMER_KEY = "068d8804da60e8b713a8980e8f22ba25"'APP ID
        OAUTH_CONSUMER_SECRET = "83e4502499dadd0a"'APP KEY
        CALLBACK_URL = "http://sorry.com/u.asp?c=db"'REDIRECT_URI
		SCOPE ="community_advanced_doumail_w,community_advanced_doumail_r,douban_basic_common,shuo_basic_r,shuo_basic_w"
    End Sub
	'人人网
	Private Sub rrInitialize      
        OAUTH_CONSUMER_KEY = "1a7506b401d14b7e828d0a69991bf547"'APP ID
        OAUTH_CONSUMER_SECRET = "a4d55680906c4c4d96bb4202ca1d838f"'APP KEY
        CALLBACK_URL = "http://sorry.com/u.asp?c=rr"'REDIRECT_URI
		SCOPE ="send_notification"
    End Sub
	
	'生成Session("State")数据.
	Public Function MakeRandNum()
		Randomize
		Dim width : width = 6 '随机数长度,默认6位
		width = 10 ^ (width - 1)
		MakeRandNum = Int((width*10 - width) * Rnd() + width)
	End Function
	
	Public Function UrlEncode(ByVal urlstr)
		urlstr = Replace(urlstr, "+", "%2B")
		urlstr = Replace(urlstr, " ", "+")
		urlstr = Replace(urlstr, "=", "%3D")
		urlstr = Replace(urlstr, "&", "%26")
		urlstr = Replace(urlstr, ":", "%3A")
		urlstr = Replace(urlstr, "/", "%2F")
		'urlstr = Replace(urlstr, "@", "%40")
		UrlEncode = urlstr
	End Function
	
	'生成登录地址
	Public Function GetAuthorization_Code(Platform)
		Dim url, params
		If Platform = "db" Then
			Call dbInitialize
			url = "https://www.douban.com/service/auth2/auth"
		ElseIf Platform = "qt" Then
			Call qtInitialize
			url = "https://open.t.qq.com/cgi-bin/oauth2/authorize"
		ElseIf Platform = "rr" Then
			Call rrInitialize
			url = "https://graph.renren.com/oauth/authorize"
		ElseIf Platform = "wb" Then
			Call wbInitialize
			url = "https://api.weibo.com/oauth2/authorize"
		Else
			response.end
		End If
		params = "client_id=" & OAUTH_CONSUMER_KEY
		params = params & "&redirect_uri=" & UrlEncode(CALLBACK_URL)
		params = params & "&response_type=code"
		params = params & "&scope="&SCOPE
		'params = params & "&state="&Session("State")
		url = url & "?" & params
		GetAuthorization_Code = (url)
	End Function
	
	
	'获取 access_token
	Public Function GetAccess_Token(Platform)
		Dim url, params,Temp
		If Platform = "db" Then
			Call dbInitialize
			url = "https://www.douban.com/service/auth2/token"
		ElseIf Platform = "qt" Then
			Call qtInitialize
			url = "https://open.t.qq.com/cgi-bin/oauth2/access_token"
		ElseIf Platform = "rr" Then
			Call rrInitialize
			url = "https://graph.renren.com/oauth/token"
		ElseIf Platform = "wb" Then
			Call wbInitialize
			url = "https://api.weibo.com/oauth2/access_token"
		Else
			response.end
		End if
	    params = "client_id=" & OAUTH_CONSUMER_KEY
		params = params & "&client_secret=" & OAUTH_CONSUMER_SECRET
		params = params & "&redirect_uri=" & UrlEncode(CALLBACK_URL)
		params = params & "&grant_type=authorization_code"
		params = params & "&code="&Session("Code")
		'params = params & "&state="&Session("State")
		Temp=PostURL(url,params)
		GetAccess_Token=Temp
	End Function
	
	'检测是否合法登录！
	Public Function CheckLogin()
		Dim Code,mState
		Code=Trim(Request.QueryString("code"))
		'mState=Trim(Request.QueryString("state"))
		If Code<>"" Then
			CheckLogin = True
			Session("Code")=Code
		Else
			CheckLogin = False
		End If
	End Function
	
	'获取openid
	Public Function Getopenid()
		Dim url, params,Temp
		url = "https://graph.qq.com/oauth2.0/me"
		params = "access_token="&Session("Access_Token")
		url = Url & "?" & params
		'response.write url
		'response.end
		Temp=OpenURL(url)
		Temp=split(Temp,"openid"":""")(1)
		Temp=split(Temp,"""}")(0)
		Getopenid=Temp
	End Function

	'发送一封私信
	Public Function addPrivate(content,name)
		Dim url, params
		Call qtInitialize
		userip = Request.ServerVariables("HTTP_X_FORWARDED_FOR") 
		If userip = "" Then userip = Request.ServerVariables("REMOTE_ADDR")
		url = "http://open.t.qq.com/api/private/add"
		params = "oauth_consumer_key=" & OAUTH_CONSUMER_KEY
		params = params & "&access_token=" & Session("Access_Token")
		params = params & "&name=" & name
		params = params & "&content="&content
		params = params & "&clientip=" & userip
		params = params & "&format=json"
		params = params & "&oauth_version=2.a"
		params = params & "&openid=" &session("openid")
		params = params & "&scope=" & scope
		response.write url&"?"&params
		'response.end
		addPrivate = PostURL(url,params)
	End Function

	'发送一条微博
	Public Function Post_Webo(content)
		Dim url, params
		url = "https://graph.qq.com/t/add_t"
		params = "oauth_consumer_key=" & OAUTH_CONSUMER_KEY
		params = params & "&access_token=" & Session("Access_Token")
		params = params & "&openid=" & Session("Openid")
		params = params & "&content="&content
		Post_Webo = RequestUrl_post(url,params)
	End Function
	
	'分享内容到QQ空间
	Public Function Post_Share(title,turl,comment,summary,images)
		Dim url, params
		url = "https://graph.qq.com/share/add_share"
		params = "oauth_consumer_key=" & OAUTH_CONSUMER_KEY
		params = params & "&access_token=" & Session("Access_Token")
		params = params & "&openid=" & Session("Openid")
		params = params & "&title="&title
		params = params & "&url="&turl
		params = params & "&title="&title
		params = params & "&comment="&comment
		params = params & "&summary="&summary
		params = params & "&images="&images
		params = params & "&format=json"
		Post_Share = RequestUrl_post(url,params)
	End Function
	
	'获取用户信息,得到一个json格式的字符串
	Public Function GetUserInfo()
		Dim url, params, result
		url = "https://graph.qq.com/user/get_user_info"
		params = "oauth_consumer_key=" & OAUTH_CONSUMER_KEY
		params = params & "&access_token=" & Session("Access_Token")
		params = params & "&openid=" & Session("Openid")
		url = url & "?" & params
		GetUserInfo = OpenURL(url)
	End Function
	
	'获取腾讯微博登录用户的用户资料,得到一个json格式的字符串
	Public Function Get_Info()
		Dim url, params, result
		url = "https://graph.qq.com/user/get_info"
		params = "oauth_consumer_key=" & OAUTH_CONSUMER_KEY
		params = params & "&access_token=" & Session("Access_Token")
		params = params & "&openid=" & Session("Openid")
		params = params & "&format=json"
		url = url & "?" & params
		Get_Info = OpenURL(url)
	End Function

	
	'获取用户名字,性别,从json字符串里截取相关字符
	Public Function GetUserName(json)
	    Dim nickname,sex
		nickname = Split(json, "nickname"":""")(1)
		sex=Split(json, "gender"":""")(1)
		nickname = Split(nickname, """,")(0)
		sex=Split(sex, """")(0)
	    GetUserName = Array(nickname,sex)
	End Function
	
	'获取腾讯微博登录用户Email,从json字符串里截取相关字符
	Public Function GetUserEmail(json)
	    Dim Email
		Email = Split(json, "email"":""")(1)
		Email = Split(Email, """,")(0)
	    GetUserEmail = Email
	End Function
End Class
%>